#!/bin/bash
cd ~/shell/lnmp_soft/  #  准备tar包放到该目录下
tar xf apache-tomcat-8.0.30.tar.gz
cp -r apache-tomcat-8.0.30 /usr/local/tomcat  #  将Tomcat放到指定目录下
cd /usr/local/tomcat
yum -y install java-1.8.0-openjdk &> /dev/null #  安装Java运行环境
mv /dev/random /dev/random.bak
ln -s /dev/urandom /dev/random  #  由于Tomcat启动需要大量随机字符，所以需要替换系统随机字符
echo "tomcat-test~~~" > webapps/ROOT/test01.html  #  创建静态测试页面
echo "<html>
<body>
<center>
Now time is: <%=new java.util.Date()%>
</center>
</body>
</html>" > webapps/ROOT/test02.jsp  #  创建动态测试页面
bin/startup.sh &> /dev/null #  开启服务
sleep 1
for i in {1..3}  #  最多尝试启动3次
do
ss -antulp | grep :8005 &> /dev/null
re=`echo $?`  #  定义变量接收返回值
ss -antulp | grep :8009 &> /dev/null
re1=`echo $?`
ss -antulp | grep :8080 &> /dev/null
re2=`echo $?`
if test $re=$re1=$re2=0;then
echo -e "\033[32m******这是静态页面******\033[0m"
curl http://127.0.0.1:8080/test01.html
echo -e "\033[32m******这是动态页面******\033[0m"
curl http://127.0.0.1:8080/test02.jsp
echo -e "\033[32m========================\033[0m"
echo -e "\033[32m***tomcat启动成功！！***\033[0m"
break  #  终止循环
else   #  重启服务
killall java
sleep 1
bin/startup.sh &> /dev/null
fi
if [ $i = 3 ];then
echo -e "\033[31mtomcat启动失败！！请手动检查安装~~\033[0m"  
fi
done

